package at.evonfc.bl;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Parcelable;

public class NfcUtils {
	public static List<NdefMessage> getMessagesFromIntent(Intent intent) {
		// initialize a new List
		List<NdefMessage> intentMessages = new ArrayList<NdefMessage>();
		String action = intent.getAction();
		if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
				|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
			
			Parcelable[] rawMsgs = intent
					.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
			if (rawMsgs != null) {
				for (Parcelable msg : rawMsgs) {
					if (msg instanceof NdefMessage) {
						intentMessages.add((NdefMessage) msg);
					}
				}
			} else {
				// Unknown tag type
				byte[] empty = new byte[] {};
				final NdefRecord record = new NdefRecord(
						NdefRecord.TNF_UNKNOWN, empty, empty, empty);
				final NdefMessage msg = new NdefMessage(
						new NdefRecord[] { record });
				intentMessages = new ArrayList<NdefMessage>() {
					{
						add(msg);
					}
				};
			}
		}
		return intentMessages;
	}
}
